import datetime
import json
import logging
import re
import time
from logging import handlers
from time import sleep

import pyttsx3
import requests
from selenium.webdriver.support import expected_conditions as EC
import colorlog
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

# 模块初始化
engine = pyttsx3.init()

# 语音播报内容
content = " 人生苦短，我用 pai sin"

# 淘宝官网
tb = 'https://www.taobao.com/'

# 结算的时间差，4s的时候点结算 单位毫秒
jiesuan = 0
# 提交订单的时间差,在允许时间的10毫秒内点击提交
tijiao = 0
# 获取淘宝时间的配置
url1 = 'http://acs.m.taobao.com/gw/mtop.common.getTimestamp/'
url2 = 'http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.43'
}

# 模拟器选项
Options = webdriver.ChromeOptions()
# 引入不关闭浏览器的相关配置项
Options.add_experimental_option("detach", True)
# 避免终端下执行代码报警告
Options.add_experimental_option("excludeSwitches", ['enable-automation', 'enable-logging'])
# 以下两行代码解决支付宝支付页面对webdriver的检测
# Options = webdriver.ChromeOptions()
# Options.add_argument("disable-blink-features=AutomationControlled")


# 日志配置
colors_config = {
    # 终端输出日志颜色配置
    'DEBUG': 'white',
    'INFO': 'cyan',
    'WARNING': 'yellow',
    'ERROR': 'red',
    'CRITICAL': 'bold_red',
}

default_formats = {
    # 终端输出格式
    'color_format': '%(log_color)s%(asctime)s.%(msecs)03d---%(levelname)s  %(message)s',
    # 日志输出格式
    'log_format': '%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'
}
formatter1 = colorlog.ColoredFormatter(default_formats["color_format"], log_colors=colors_config,
                                       datefmt='%Y-%m-%d %H:%M:%S')
formatter2 = logging.Formatter(default_formats["log_format"])
# banner配置
yellow = '\033[01;33m'
white = '\033[01;37m'
green = '\033[01;32m'
blue = '\033[01;34m'
red = '\033[1;31m'
end = '\033[0m'
banner = f'''项目启动
{yellow}
宝可梦计划 #V0.0.1
-------------------------------------------------------------------------
{green}
 ██████                      ██   ██         ████     ████                         
░█░░░░██                    ░██  ██         ░██░██   ██░██                   █████ 
░█   ░██   ██████    ██████ ░██ ██    █████ ░██░░██ ██ ░██  █████  ███████  ██░░░██
░██████   ░░░░░░██  ██░░░░██░████    ██░░░██░██ ░░███  ░██ ██░░░██░░██░░░██░██  ░██
░█░░░░ ██  ███████ ░██   ░██░██░██  ░███████░██  ░░█   ░██░███████ ░██  ░██░░██████
░█    ░██ ██░░░░██ ░██   ░██░██░░██ ░██░░░░ ░██   ░    ░██░██░░░░  ░██  ░██ ░░░░░██
░███████ ░░████████░░██████ ░██ ░░██░░██████░██        ░██░░██████ ███  ░██  █████ 
░░░░░░░   ░░░░░░░░  ░░░░░░  ░░   ░░  ░░░░░░ ░░         ░░  ░░░░░░ ░░░   ░░  ░░░░░  

'''


# 获取淘宝时间戳 13位 毫秒级
def get_time():
    get_data = requests.get(url1, headers=headers)
    data = json.loads(get_data.text)
    return int(data['data']['t']) + 45

# 使用本地时间
def get_localtime():
    t = time.time()
    return int(t * 1000)

# 日志定义
def logdefine():
    logger = logging.getLogger()  # 创建记录器
    logger.setLevel(level=logging.DEBUG)  # 设置记录级别

    # 输出到控制台
    stream_handler = logging.StreamHandler()  # 输出流位置
    stream_handler.setLevel(logging.INFO)  # 输出级别
    stream_handler.setFormatter(formatter1)  # 输出格式

    # 保存日志文件，并自动分割器，按文件大小分割
    rotating_file_handler = handlers.RotatingFileHandler(filename='./日志.log', mode='a', maxBytes=10 * 1024 * 1024,
                                                         backupCount=2, encoding='utf-8')
    rotating_file_handler.setLevel(logging.INFO)
    rotating_file_handler.setFormatter(formatter2)

    logger.addHandler(rotating_file_handler)  # 添加分割器的日志输出
    logger.addHandler(stream_handler)  # 设置输出流到控制台
    return logger


# 判断是否为空
def isNotEmpty(variable):
    if isinstance(variable, str) and variable == "":
        return False
    else:
        return True


# 时间格式化
def timeFormat(a):
    stime = datetime.date.today().strftime(f'''%Y-%m-%d {a}:%S.%f''')
    return stime


def formatTime(timestamp):
    dt_object = datetime.datetime.fromtimestamp(timestamp / 1000)  # 将毫秒级时间戳转换为秒级时间戳
    formatted_time = dt_object.strftime('%Y-%m-%d %H:%M:%S')
    # 格式化时间
    return formatted_time


# 主函数
def main():
    t = get_time()
    logger.info("淘宝时间{}".format(t))
    logger.info("提交时间{}".format(formatTime(t)))

# 原神
if __name__ == "__main__":
    # 创建日志
    logger = logdefine()
    main()
